package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yuwj.chinaa.R;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.SharedpreferenceUtil;
import com.yuwj.chinaa.util.ToastUtil;
import com.yuwj.chinaa.util.ValidatonCheckUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 账户修改支付密码界面
 */
public class AccountUpdatePaymentPasswordActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_userphone)
    TextView tvUsername;
    @BindView(R.id.et_authentication_code)
    EditText etAuthenticationCode;
    @BindView(R.id.tv_send_verification_code)
    TextView tvSendVerificationCode;
    @BindView(R.id.et_new_passwrod)
    EditText etNewPasswrod;
    @BindView(R.id.et_new_passwrod2)
    EditText etNewPasswrod2;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;

    IPresenter presenter;

    private CountDownTimer timer = new CountDownTimer(1000 * 60, 1000) {
        //参数1，设置倒计时的总时间（毫秒）
        //参数2，设置每次减去多少毫秒
        @Override
        public void onTick(long millisUntilFinished) {
            tvSendVerificationCode.setEnabled(false);
            tvSendVerificationCode.setText((millisUntilFinished / 1000) + "s");
        }

        @Override
        public void onFinish() {
            tvSendVerificationCode.setEnabled(true);
            tvSendVerificationCode.setText("发送验证码");
        }
    };

    public static Intent createIntent(Context context) {
        return new Intent(context, AccountUpdatePaymentPasswordActivity.class);
    }

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_account_update_payment_password;
    }

    @Override
    public void initParam() {
        presenter = new PresenterImpl(this);
    }

    @Override
    public void initView() {
        tvTitle.setText("修改支付密码");
        tvUsername.setText(entityUser.getUserPhone());
    }

    @Override
    public void initOperator() {

    }

    @Override
    public void onDestroy() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        super.onDestroy();
    }


    @OnClick({R.id.iv_back,R.id.tv_send_verification_code, R.id.tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
            onBackPressed();
            break;
            case R.id.tv_send_verification_code:
                String phone1 = tvUsername.getText().toString();
                if (TextUtils.isEmpty(phone1) || !ValidatonCheckUtil.isMobile(phone1)) {
                    ToastUtil.getInstance().shortLength(context, "请输入有效的手机号");
                    return;
                }
                // 发送短信验证码
                JSONObject paramWithSendVerifyCode = new JSONObject();
                try {
                    paramWithSendVerifyCode.put("phone", phone1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                presenter.msgGetVerifyCode(paramWithSendVerifyCode.toString());
                timer.start();
                break;
            case R.id.tv_submit:
                String phone = tvUsername.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.getInstance().shortLength(context, "当前用户手机号为空");
                    return;
                }
                String verifyCode = etAuthenticationCode.getText().toString();
                if (TextUtils.isEmpty(verifyCode)) {
                    ToastUtil.getInstance().shortLength(context, "验证码不能为空");
                    return;
                }
                String password = etNewPasswrod.getText().toString();
                String password2 = etNewPasswrod2.getText().toString();

                if (TextUtils.isEmpty(password) || TextUtils.isEmpty(password2)) {
                    ToastUtil.getInstance().shortLength(context, "两次密码不能为空");
                    return;
                }
                if (!password.equals(password2)) {
                    ToastUtil.getInstance().shortLength(context, "两次密码不同");
                    return;
                }
                if (!ValidatonCheckUtil.isLegalPassword(password)) {
                    ToastUtil.getInstance().shortLength(context, "请输入6位及以上字符密码");
                    return;
                }
                JSONObject paramWithResetPayPassword = new JSONObject();
                try {
                    paramWithResetPayPassword.put("newwithpwd", password);
                    paramWithResetPayPassword.put("verifycode", verifyCode);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                presenter.userSetPayPassword(paramWithResetPayPassword.toString());

                break;
        }
    }

    @Override
    public void onUserSetPayPasswordSuccess(String resp) {
        super.onUserSetPayPasswordSuccess(resp);
        ToastUtil.getInstance().shortLength(context,"修改成功");
        entityUser.setWithdrawPwd(resp);
        SharedpreferenceUtil.getInstance().saveUserInfo(context,entityUser);
        finish();
    }

    @Override
    public void onUserSetPayPasswordFail(Exception e) {
        super.onUserSetPayPasswordFail(e);
        ToastUtil.getInstance().shortLength(context,""+e.getMessage());
    }


    @Override
    public void onMsgGetVerifyCodeSuccess(String code) {
        super.onMsgGetVerifyCodeSuccess(code);
        ToastUtil.getInstance().shortLength(context, "验证码发送成功");
    }

    @Override
    public void onMsgGetVerifyCodeFail(Exception e) {
        super.onMsgGetVerifyCodeFail(e);
        ToastUtil.getInstance().shortLength(context, "" + e.getMessage());
    }
}
